package GUI;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;

import GUI_CONTROLER.*;
import start.*;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;
import javax.swing.JTextPane;

public class UpdatedLoginUser extends JFrame {
	private JTextField textField;
	private JTextField textField_1;
	ClientConsole m_chat;
	String user,pass;
	public int flag=-1;
	public DBhandler log;
	
	Gui_Student_Entry_Window StuMain = new Gui_Student_Entry_Window();
	LoginUserPanel LoginP = new LoginUserPanel();
	
	public void exit()
	{
		this.setVisible(false);
	}
	
	public UpdatedLoginUser(ClientConsole chat)
	{
		m_chat=chat;
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setAlwaysOnTop(false);
		setTitle("Login Window");
		setSize(800,800);
		setBounds(0, 0, 800, 800);
		setContentPane(LoginP);
		
		final LoginUserC Controler = new LoginUserC(chat);
		
		
		LoginP.btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(e.getSource()=="cancel")
					System.exit(0);
					/* -= Eli =-Need to Add close window option */
				else
				{
					flag=1;
					try {
						int answer=0;
						user=LoginP.textField.getText();
						pass=LoginP.textField_1.getText();
						answer = Controler.HandleLoginRequestFromGui(user, pass);
						int k=2;
						if(100 == answer)
						{
							JOptionPane.showMessageDialog(null, "User ID does not exists in the system..");
						}
						else if(99 == answer)
						{
							JOptionPane.showMessageDialog(null,
								    "The password does not match the user name..",
								    "Login error",
								    JOptionPane.WARNING_MESSAGE);
						}
						else
							{
							JOptionPane.showMessageDialog(null, "Login Succesfull..!");
								switch(answer)
								{
								case 1:
								{
									LoginP.setVisible(false);
									StuMain.m_UserID = user;
									StuMain.m_chat = m_chat;
									//StuMain.setVisible(true);
									setContentPane(StuMain);
								}
								/* Eli: Here we will switch content pane to user main window according to the
								 * type returned from the database for that user..
								 */
								
								default:
									break;
								}
							}
						
						
						//log=new DBhandler();
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
	}

		});
	
	}
}
